﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class _Class : System.Web.UI.Page
{

    private string classname;
    private string username;

    protected void Page_Load(object sender, EventArgs e)
    {

        //Passes the name from the GET header from the index.aspx page. (cs####)
        classname = System.Web.HttpContext.Current.Request.Params.Get("name");
        username = HttpContext.Current.User.Identity.Name.ToString();
        System.Web.HttpContext.Current.Request.Cookies.Set(new HttpCookie("classname", classname));
        if (classname != null)
            TitleLabel.Text = classname;

        //If this page is invalidly accessed send them back to Admin
        if (classname == null || classname.Trim().Equals(""))
        {
            System.Web.HttpContext.Current.Response.Redirect("~/index.aspx");
        }

        //Set the 'My Score' Column
        populateAverageBoxes();
        if (username.Equals(""))
        {
            myBook.Visible = false;
            myDifficulty.Visible = false;
            myFun.Visible = false;
            myHours.Visible = false;
            myLecture.Visible = false;
            myMaterial.Visible = false;
            myOverall.Visible = false;
            myProfessor.Visible = false;
        }
        else
            populateUsersBoxes();

        //Make the reply button invisible if it is not needed
        if (!DetailsView2.Visible || username.Equals(""))
            replyButton.Visible = false;
        else
            replyButton.Visible = true;

        if (DetailsView2.Visible && (HttpContext.Current.User.IsInRole("Admin") || HttpContext.Current.User.IsInRole("Professor")))
            deleteButton.Visible = true;
        else
            deleteButton.Visible = false;
    }

    private void populateAverageBoxes()
    {
        Label0.Text = DetailsView1.Rows[0].Cells[1].Text;
        Label1.Text = DetailsView1.Rows[1].Cells[1].Text;
        Label2.Text = DetailsView1.Rows[2].Cells[1].Text;
        Label3.Text = DetailsView1.Rows[3].Cells[1].Text;
        Label4.Text = DetailsView1.Rows[4].Cells[1].Text;
        Label5.Text = DetailsView1.Rows[5].Cells[1].Text;
        Label6.Text = DetailsView1.Rows[6].Cells[1].Text;
        Label7.Text = DetailsView1.Rows[7].Cells[1].Text;
    }

    private void populateUsersBoxes()
    {
        DataSetTableAdapters.GetUserScoreTableAdapter adapter = new DataSetTableAdapters.GetUserScoreTableAdapter();
        DataSet.GetUserScoreDataTable results = adapter.GetUsersScores(username, classname);

        if (results.Rows.Count <= 0)
        {
            if (myHours.Text.Equals(""))
                myHours.Text = "0";
            if (myDifficulty.Text.Equals(""))
                myDifficulty.Text = "0";
            if (myProfessor.Text.Equals(""))
                myProfessor.Text = "0";
            if (myBook.Text.Equals(""))
                myBook.Text = "0";
            if (myLecture.Text.Equals(""))
                myLecture.Text = "0";
            if (myMaterial.Text.Equals(""))
                myMaterial.Text = "0";
            if (myFun.Text.Equals(""))
                myFun.Text = "0";
            if (myOverall.Text.Equals(""))
                myOverall.Text = "0";
        }
        else
        {
            DataSet.GetUserScoreRow row = results.Rows[0] as DataSet.GetUserScoreRow;
            if (myHours.Text.Equals(""))
                myHours.Text = row.Homeworkperweek.ToString();
            if (myDifficulty.Text.Equals(""))
                myDifficulty.Text = row.Difficulty.ToString();
            if (myProfessor.Text.Equals(""))
                myProfessor.Text = row.Professor.ToString();
            if (myBook.Text.Equals(""))
                myBook.Text = row.TextBook.ToString();
            if (myLecture.Text.Equals(""))
                myLecture.Text = row.Lecture.ToString();
            if (myMaterial.Text.Equals(""))
                myMaterial.Text = row.Importance.ToString();
            if (myFun.Text.Equals(""))
                myFun.Text = row.FunFactor.ToString();
            if (myOverall.Text.Equals(""))
                myOverall.Text = row.Overall.ToString();
        }
    }

    //Activated once something tries to use ObjectDataSource1 so this is probably WORTHLESS
    protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        //Obsolete
    }
    protected void ChangeMyScoreButton_Click(object sender, EventArgs e)
    {
        //Check the values
        try
        {
            bool valid = true;
            if (Convert.ToDecimal(myHours.Text) < 0)
                valid = false;
            if (Convert.ToDecimal(myDifficulty.Text) < 0 || Convert.ToDecimal(myDifficulty.Text) > 10)
                valid = false;
            if (Convert.ToDecimal(myProfessor.Text) < 0 || Convert.ToDecimal(myProfessor.Text) > 10)
                valid = false;
            if (Convert.ToDecimal(myBook.Text) < 0 || Convert.ToDecimal(myBook.Text) > 10)
                valid = false;
            if (Convert.ToDecimal(myLecture.Text) < 0 || Convert.ToDecimal(myLecture.Text) > 10)
                valid = false;
            if (Convert.ToDecimal(myMaterial.Text) < 0 || Convert.ToDecimal(myMaterial.Text) > 10)
                valid = false;
            if (Convert.ToDecimal(myFun.Text) < 0 || Convert.ToDecimal(myFun.Text) > 10)
                valid = false;
            if (Convert.ToDecimal(myOverall.Text) < 0 || Convert.ToDecimal(myOverall.Text) > 10)
                valid = false;
            //Store the values
            if (valid)
            {
                ScoreMessage.Text = "Thank you for your input";
                commitToDatabase();
            }
            else
                ScoreMessage.Text = "Invalid input";
        }
        catch (Exception except)
        {
            ScoreMessage.Text = "Invalid input";
        }
    }

    private void commitToDatabase()
    {
        int hours = int.Parse(myHours.Text);
        int diff = int.Parse(myDifficulty.Text);
        int proff = int.Parse(myProfessor.Text);
        int book = int.Parse(myBook.Text);
        int lecture = int.Parse(myLecture.Text);
        int material = int.Parse(myMaterial.Text);
        int fun = int.Parse(myFun.Text);
        int overall = int.Parse(myOverall.Text);

        try
        {
            DataSetTableAdapters.ScoreTableAdapter Insertadapter = new DataSetTableAdapters.ScoreTableAdapter();

            DataSetTableAdapters.GetUserScoreTableAdapter adapter = new DataSetTableAdapters.GetUserScoreTableAdapter();
            DataSet.GetUserScoreDataTable results = adapter.GetUsersScores(username, classname);
            if (results.Count == 0)
                Insertadapter.Insert(classname, username, hours, diff, proff, book, lecture, material, fun, overall);
            else
                Insertadapter.Update(hours, diff, proff, book, lecture, material, fun, overall, classname, username);
        }
        catch (Exception e)
        {
            //unable to update/insert
        }

        DetailsView1.DataBind();
        populateAverageBoxes();

    }

    protected void submitEvalButton_Click(object sender, EventArgs e)
    {
        string classname = System.Web.HttpContext.Current.Request.Params.Get("name");
        System.Web.HttpContext.Current.Response.Redirect("Submit.aspx?action=eval&name=" + classname);
    }
    protected void submitPostButton_Click(object sender, EventArgs e)
    {
        string classname = System.Web.HttpContext.Current.Request.Params.Get("name");
        System.Web.HttpContext.Current.Response.Redirect("Submit.aspx?action=post&name=" + classname);
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //Obsolete
    }
    protected void GridView1_SelectedIndexChanging(object sender, EventArgs e)
    {
        GridViewSelectEventArgs action = (GridViewSelectEventArgs)e;

        string id = (GridView1.Rows[action.NewSelectedIndex].FindControl("Label1") as Label).Text;

        IDlabel.Text = id;
        DetailsView2.Visible = true;

        if (!username.Equals(""))
            replyButton.Visible = true;
        if (HttpContext.Current.User.IsInRole("Admin") || HttpContext.Current.User.IsInRole("Professor"))
            deleteButton.Visible = true;
    }
    protected void GridView2_SelectedIndexChanging(object sender, EventArgs e)
    {
        GridViewSelectEventArgs action = (GridViewSelectEventArgs)e;

        string id = (GridView2.Rows[action.NewSelectedIndex].FindControl("Label1") as Label).Text;

        EvalLabel.Text = id;
        GridView4.Visible = true;
        if (HttpContext.Current.User.IsInRole("Admin") || HttpContext.Current.User.IsInRole("Professor"))
            evalDeleteButton.Visible = true;
    }
    protected void replyButton_Click(object sender, EventArgs e)
    {
        string classname = System.Web.HttpContext.Current.Request.Params.Get("name");
        System.Web.HttpContext.Current.Response.Redirect("Submit.aspx?action=post&name=" + classname + "&replyid=" + IDlabel.Text);
    }
    protected void deleteButton_Click(object sender, EventArgs e)
    {
        //Delete this Post
        DataSetTableAdapters.InsertPostTableAdapter adapter = new DataSetTableAdapters.InsertPostTableAdapter();
        adapter.Update("Archive", int.Parse((DetailsView2.Rows[0].FindControl("Label1") as Label).Text));

        System.Web.HttpContext.Current.Response.Redirect("Class.aspx?name=" + classname);
    }

    protected void deleteEvalButton_Click(object sender, EventArgs e)
    {
        //Delete this Evaluation
        DataSetTableAdapters.InsertReviewTableAdapter adapter = new DataSetTableAdapters.InsertReviewTableAdapter();
        adapter.Update(int.Parse(EvalLabel.Text));

        System.Web.HttpContext.Current.Response.Redirect("Class.aspx?name=" + classname);
    }    
}
